package bylw.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import bylw.dao.DrugDao;
import bylw.dao.UrlDao;
import bylw.entity.Drug;
import bylw.entity.Url;
import bylw.service.Service;
import bylw.util.DrugHttpClient;
import bylw.util.HibernatePageModel;

@org.springframework.stereotype.Service
public class ServiceImpl implements Service {
	private DrugDao drugDao;
	private UrlDao urlDao;

	@Autowired
	public void setDrugDao(DrugDao drugDao) {
		this.drugDao = drugDao;
	}

	@Autowired
	public void setUrlDao(UrlDao urlDao) {
		this.urlDao = urlDao;
	}

	/*
	 * 查询药品
	 */
	public List<Drug> getSearchResult(String name) {
		// List<Drug>用来存在搜索的结果
		List<Drug> drugList = new ArrayList<Drug>();

		// 先从本地数据库中获取数据，然后再从接口中查询
		Drug dbDrug = this.drugDao.findDrug(name);
		if (null != dbDrug) {
			drugList.add(dbDrug);
		}

		DrugHttpClient drugHttpClient = new DrugHttpClient();
		List<Url> urlList = this.urlDao.getUrlList();
		/*
		 * 该方法先取得url，然后替换${name}，然后提交请求，解析请求的结果
		 */
		drugHttpClient.getDrugList(name, urlList, drugList);

		return drugList;
	}

	/*
	 * 查询药品列表
	 */
	public HibernatePageModel getDrugList(int pageNo, int pageSize) {
		return this.drugDao.getDrugList(pageNo, pageSize);
	}

	/*
	 * 查询URL列表
	 */
	public HibernatePageModel getUrlList(int pageNo, int pageSize) {
		return this.urlDao.getUrlList(pageNo, pageSize);
	}

	/*
	 * 增加Drug
	 */
	public void addDrug(Drug drug) {
		this.drugDao.addDrug(drug);
	}

	/*
	 * 增加Url
	 */
	public void addUrl(Url url) {
		this.urlDao.addUrl(url);
	}

	/*
	 * 跟新Url
	 */
	public void updateUrl(Url url) {
		this.urlDao.update(url);
	}

	/*
	 * 删除Url
	 */
	public void deleteUrl(int id) {
		this.urlDao.delete(id);
	}

}
